package miage.m1.poo.arborescence;
import java.awt.*;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;


public class ParcoursJTree extends JFrame{
	private DefaultMutableTreeNode racine;
	private JTree tree;
	private JPanel jp;
	private int count = 0;
	private JLabel[] jl = new JLabel[100]; 
	private Button[] b = new Button[100];
	
	public ParcoursJTree(String nom){
		super(nom);
		arborescence(new File("/Users/hlaffineur/Desktop"));
		pack();
		setVisible(true);
	}

	public void arborescence(File path) {
		racine = new DefaultMutableTreeNode(path.getName());
		tree = new JTree(racine);
		((DefaultTreeModel) tree.getModel()).setRoot(racine);
		((DefaultTreeModel) tree.getModel()).reload();
		File[] files = path.listFiles();
		for(int i=0;i<files.length;i++){
			recursive(files[i],racine);
			/*DefaultMutableTreeNode node = new DefaultMutableTreeNode(files[i].getName());
			racine.add(node);
			try{
				File[] listeFiles = files[i].listFiles();
				for(int j=0;j<listeFiles.length;j++){
					recursive(listeFiles[i],node);	
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}*/
		}
		DefaultTreeModel model = new DefaultTreeModel(racine);
	    tree.setModel(model);


	    // ajout du JTree au formulaire
	    
	    JScrollPane scrollpane = new JScrollPane();
        getContentPane().add(scrollpane, BorderLayout.CENTER);
        scrollpane.getViewport().add(tree, null);
        validate();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
	
	public void recursive(File f, DefaultMutableTreeNode node){
		if(f.isDirectory()){
			DefaultMutableTreeNode child = new DefaultMutableTreeNode(f.getName());
			node.add(child);
			for(File children : f.listFiles()){
				recursive(children, child);
			}
		}
		else if(f.isFile()){
			node.add(new DefaultMutableTreeNode(f.getName()));
		}
	}
	
	public static void main(String[] arg){
		ParcoursJTree pj = new ParcoursJTree("Arborescence");
	}

}
